package cn.gdcp.bisai;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

public class ContentProviderDB extends ContentProvider {
    private static final String TAG = "ContentProviderDB";
    private SQLiteOpenHelperDB sqLiteOpenHelperDB;
    private static  String AUTOHORITY="com.sql";
    private  static UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
    static {
        uriMatcher.addURI(AUTOHORITY,"student",1);
        uriMatcher.addURI(AUTOHORITY,"score",2);
        uriMatcher.addURI(AUTOHORITY,"score1",3);
    }

    @Override
    public boolean onCreate() {
        sqLiteOpenHelperDB=new SQLiteOpenHelperDB(getContext(),"stu.db",null,1);
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        switch (uriMatcher.match(uri)){
            case 1:
                SQLiteDatabase sqLiteDatabase=sqLiteOpenHelperDB.getReadableDatabase();
                Cursor cursor=sqLiteDatabase.query("student",strings,s,strings1
                        ,null,null,s1);
                return  cursor;

            case 2:
                SQLiteDatabase sqLiteDatabase1=sqLiteOpenHelperDB.getReadableDatabase();
                Cursor cursor1=sqLiteDatabase1.query("score",strings,s,strings1,null,null,s1);
                return cursor1;
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        switch (uriMatcher.match(uri)){
            case 1:
                SQLiteDatabase sqLiteDatabase=sqLiteOpenHelperDB.getWritableDatabase();
                sqLiteDatabase.insert("student",null,contentValues);
                sqLiteDatabase.close();
                break;

            case 2:
                SQLiteDatabase sqLiteDatabase1=sqLiteOpenHelperDB.getWritableDatabase();
                sqLiteDatabase1.insert("score",null,contentValues);
                sqLiteDatabase1.close();
                break;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String where, @Nullable String[] whereArgs) {
        switch (uriMatcher.match(uri)){
            case 1:
                SQLiteDatabase sqLiteDatabase=sqLiteOpenHelperDB.getWritableDatabase();
                sqLiteDatabase.delete("student",where,whereArgs);
                sqLiteDatabase.close();
                break;
            case 2:
                SQLiteDatabase sqLiteDatabase1=sqLiteOpenHelperDB.getWritableDatabase();
                sqLiteDatabase1.delete("score",where,whereArgs);
                sqLiteDatabase1.close();
                break;
        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String whereclause, @Nullable String[] whereArgs) {
         switch (uriMatcher.match(uri)){
             case 1:
                 SQLiteDatabase sqLiteDatabase=sqLiteOpenHelperDB.getWritableDatabase();
                 sqLiteDatabase.update("student",contentValues,whereclause,whereArgs);
                 sqLiteDatabase.close();
                 break;
             case 2:
                 SQLiteDatabase sqLiteDatabase1=sqLiteOpenHelperDB.getWritableDatabase();
                 sqLiteDatabase1.update("score",contentValues,whereclause,whereArgs);
                 sqLiteDatabase1.close();
                 break;
         }

        return 0;
    }
}
